import {
  SET_ADDRESS_INFO,
  SET_USER_INFO,
  SET_WEIXIN_INFO,
  SET_TOKEN,
  INIT_USER_INFO,
  CLEAR_TOKEN,
  SET_ADDED_ADDRESS,
  SET_GROUP_GOODS,
  RECORD_ADDRESS
} from "./mutation-types.js";

import { setStore, getStore,removeStore } from "src/service/common";

export default {
  // 记录选择地址
  [SET_ADDRESS_INFO](state, addressInfo) {
    state.myAddressInfo = addressInfo;
  },

  // 记录用户信息
  [SET_USER_INFO](state, userInfo) {
    state.userInfo = userInfo;
    setStore("userInfo", state.userInfo);
  },

  // 记录微信用户信息 openid、unionid、nickname、headImg
  [SET_WEIXIN_INFO](state, wxUserInfo) {
    state.wxUserInfo = wxUserInfo;
    setStore("wxUserInfo", state.wxUserInfo);
  },

  //设置token
  [SET_TOKEN](state, token){
    state.token = token;
    setStore("token", state.token);
  },

  //清除token
  [CLEAR_TOKEN](state){
    state.token = '';
    removeStore('token');
  },

  //网页初始化时从本地获取用户信息token、以及 wxUserInfo
  [INIT_USER_INFO](state) {
    let wxUserInfo = getStore("wxUserInfo");
    let userInfo = getStore("userInfo");
    let token = getStore("token");
    if (wxUserInfo) {
      state.wxUserInfo = JSON.parse(wxUserInfo);
    }
    if (userInfo) {
      state.userInfo = JSON.parse(userInfo);
    }
    if (token) {
      state.token = token;
    }
  },

  // 存储添加地址界面数据
  [SET_ADDED_ADDRESS](state, address) {
    state.addedAddress = address;
  },

  // 存储团购商品
  [SET_GROUP_GOODS](state, goods) {
    state.groupGoods = goods;
  },

  // 记录当前经度纬度
	[RECORD_ADDRESS](state, {lat,lng}) {
		state.lat = lat;
		state.lng = lng;
	},
};
